package com.bz.xtcx.manager.mapper;

import java.util.List;

import com.bz.xtcx.manager.mapper.provider.CmsExchangesProvider;
import com.bz.xtcx.manager.persist.conditions.ExchangeCondition;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import com.bz.xtcx.manager.entity.CmsExchanges;
import com.bz.xtcx.manager.vo.VoQueryExchanges;
import com.bz.xtcx.manager.vo.VoTaskSum;

public interface CmsExchangesMapper {

    @Insert("insert into `cms_exchanges`(id, title, subject, type_id, cover, ex_addr, enroll_start, enroll_end, ex_start, ex_end, organizer_name,"
            + " org_contacts, org_phone, org_email, org_addr, publish_user_id, publish_now, publish_date, stick_sort, ex_content, check_user_id, check_date, check_status, readable, creater, updater)"
            + " VALUES(#{id, jdbcType=VARCHAR},"
            + " #{title, jdbcType=VARCHAR},"
            + " #{subject, jdbcType=VARCHAR},"
            + " #{typeId, jdbcType=INTEGER},"
            + " #{cover, jdbcType=VARCHAR},"
            + " #{exAddr, jdbcType=VARCHAR},"
            + " #{enrollStart, jdbcType=DATE},"
            + " #{enrollEnd, jdbcType=DATE},"
            + " #{exStart, jdbcType=DATE},"
            + " #{exEnd, jdbcType=DATE},"
            + " #{organizerName, jdbcType=VARCHAR},"
            + " #{orgContacts, jdbcType=VARCHAR},"
            + " #{orgPhone, jdbcType=VARCHAR},"
            + " #{orgEmail, jdbcType=VARCHAR},"
            + " #{orgAddr, jdbcType=VARCHAR},"
            + " #{publishUserId, jdbcType=VARCHAR},"
            + " #{publishNow, jdbcType=INTEGER},"
            + " #{publishDate, jdbcType=DATE},"
            + " #{stickSort, jdbcType=INTEGER},"
            + " #{exContent, jdbcType=VARCHAR},"
            + " #{checkUserId, jdbcType=VARCHAR},"
            + " #{checkDate, jdbcType=DATE},"
            + " #{checkStatus, jdbcType=INTEGER},"
            + " #{readable, jdbcType=INTEGER},"
            + " #{creater, jdbcType=VARCHAR}, #{updater, jdbcType=VARCHAR})"
            )
    @SelectKey(before = true, keyProperty = "id", resultType = String.class, statementType = StatementType.STATEMENT, statement="select uuid()")
    int insert(CmsExchanges e);
    
    @Delete("delete from `cms_exchanges` where id = #{id}")
    int del(String id);
    
    @Update("update `cms_exchanges` set title=#{title, jdbcType=VARCHAR},"
            + " subject=#{subject, jdbcType=VARCHAR},"
            + " type_id=#{typeId, jdbcType=INTEGER},"
            + " cover=#{cover, jdbcType=VARCHAR},"
            + " ex_addr=#{exAddr, jdbcType=VARCHAR},"
            + " enroll_start=#{enrollStart, jdbcType=DATE},"
            + " enroll_end=#{enrollEnd, jdbcType=DATE},"
            + " ex_start=#{exStart, jdbcType=DATE},"
            + " ex_end=#{exEnd, jdbcType=DATE},"
            + " organizer_name=#{organizerName, jdbcType=VARCHAR},"
            + " org_contacts=#{orgContacts, jdbcType=VARCHAR},"
            + " org_phone=#{orgPhone, jdbcType=VARCHAR},"
            + " org_email=#{orgEmail, jdbcType=VARCHAR},"
            + " org_addr=#{orgAddr, jdbcType=VARCHAR},"
            + " publish_user_id=#{publishUserId, jdbcType=VARCHAR},"
            + " publish_now=#{publishNow, jdbcType=INTEGER},"
            + " publish_date=#{publishDate, jdbcType=DATE},"
            + " stick_sort=#{stickSort, jdbcType=INTEGER},"
            + " ex_content=#{exContent, jdbcType=VARCHAR},"
            + " check_user_id=#{checkUserId, jdbcType=VARCHAR},"
            + " check_date=#{checkDate, jdbcType=DATE},"
            + " check_status=#{checkStatus, jdbcType=INTEGER},"
            + " readable=#{readable, jdbcType=INTEGER},"
            + " updater=#{updater, jdbcType=VARCHAR}"
            + " where id=#{id}")
    int update(CmsExchanges e);
    
    @Update("update `cms_exchanges` set readable=#{readable, jdbcType=INTEGER} where id=#{id}")
    int updateReadable(@Param("id")String id, @Param("readable")int readable);
    
    @Update("update `cms_exchanges` set stick_sort=#{top, jdbcType=INTEGER} where id=#{id}")
    int updateStickSort(@Param("id")String id, @Param("top")int top);
    
    @Select("select * from `cms_exchanges` where id = #{id}")
    @Results(
            id = "cmsExchangesRow",
            value = {
                @Result(id = true, property = "id", column = "id"),
                @Result(property = "title", column = "title"),
                @Result(property = "subject", column = "subject"),
                @Result(property = "typeId", column = "type_id"),
                @Result(property = "cover", column = "cover"),
                @Result(property = "exAddr", column = "ex_addr"),
                @Result(property = "enrollStart", column = "enroll_start"),
                @Result(property = "enrollEnd", column = "enroll_end"),
                @Result(property = "exStart", column = "ex_start"),
                @Result(property = "exEnd", column = "ex_end"),
                @Result(property = "organizerName", column = "organizer_name"),
                @Result(property = "orgContacts", column = "org_contacts"),
                @Result(property = "orgPhone", column = "org_phone"),
                @Result(property = "orgEmail", column = "org_email"),
                @Result(property = "orgAddr", column = "org_addr"),
                @Result(property = "publishNow", column = "publish_now"),
                @Result(property = "publishDate", column = "publish_date"),
                @Result(property = "publishUserId", column = "publish_user_id"),
                @Result(property = "checkDate", column = "check_date"),
                @Result(property = "checkUserId", column = "check_user_id"),
                @Result(property = "stickSort", column = "stick_sort"),
                @Result(property = "exContent", column = "ex_content"),
                @Result(property = "checkStatus", column = "check_status"),
                @Result(property = "readable", column = "readable"),
                @Result(property = "creater", column = "creater"),
                @Result(property = "createTime", column = "create_time"),
                @Result(property = "updater", column = "updater"),
                @Result(property = "updateTime", column = "update_time"),
                @Result(property = "cmsFileList", column = "id", many = @Many(select = "com.bz.xtcx.manager.mapper.CmsFileMapper.fingByOwnerId") ),
                @Result(property = "fileName", column = "file_name"),
                @Result(property = "savePath", column = "save_path"),
                @Result(property = "reqRelUrl", column = "req_rel_url"),
                @Result(property = "fileId", column = "file_id"),
                @Result(property = "allowEnroll", column = "allow_enroll"),
                @Result(property = "info", column = "info"),
                @Result(property = "checker", column = "checker"),
                @Result(property = "checkTime", column = "check_time")
            }
        )
    CmsExchanges findById(String id);

    @SelectProvider(type = CmsExchangesProvider.class, method = "findByCondition")
    List<CmsExchanges> findByCondition(VoQueryExchanges e);

    /*
     * Add By Wangtao - start
     */
    @SelectProvider(type = CmsExchangesProvider.class, method = "countByConditionProvider")
    int countByCondition(ExchangeCondition ec);

    @SelectProvider(type = CmsExchangesProvider.class, method = "selectByConditionProvider")
    @ResultMap(value = "cmsExchangesRow")
    List<CmsExchanges> selectByCondition(ExchangeCondition ec);

    @SelectProvider(type = CmsExchangesProvider.class, method = "selectByPrimaryKeyProvider")
    @ResultMap(value = "cmsExchangesRow")
    List<CmsExchanges> selectByPrimaryKey(@Param("id") String id, @Param("userId") String userId);
    
    @Select("select 1 formType,count(*) count from `cms_exchanges` where check_status = 0 union select 2 formType,count(*) count from `cms_content` where check_status = 0")
    List<VoTaskSum> taskAllDoingSum();

    @Select("select 1 formType,count(*) count from `cms_exchanges` where publish_user_id = #{userId} and check_status = 0 union select 2 formType,count(*) count from `cms_content` where publish_user_id = #{userId} and check_status = 0")
    List<VoTaskSum> taskDoingSum(@Param("userId") String userId);
}
